var app = require('../app'),
    i=0,
    lock=false,
    lockfas = false;
toastr.options.timeOut = 3000;

//案件分配
var arrcaseid = [];
//解析地址栏
function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}
$('body')
    .on('click', '.checkall', function(){
        //全选
        $(".j-check,.checkall").prop("checked", $(this).prop("checked"));

    })
    //反选
    .on('change','j-check', function(){
        if ($(".j-check:checked").length == $(".j-check").length) {
            $(".checkall").prop("checked", true);
        } else {
            $(".checkall").prop("checked", false);
        }
    })

    // 案件转移
    .on('click', '.distribution_transfer', function(){
        if(lockfas)return;
        lockfas = true;
        arrcaseid = [];
        if($("#transfer_case_ids").val()==0){
            toastr.error('请选择案件');
            return false;
        }
        $.ajax({
            url:"/casecenter/transferCase",
            method:"post",
            data:$("#transfer_form").serialize(),
            success:function(res){
                if(res.code==200){
                    lockfas = false;
                    toastr.success("转移案件成功");
                    setTimeout(function(){
                        window.location.reload();
                    },2000)
                }else{
                    lockfas = false;
                    toastr.error(res.message);

                }
            }
        })
        lockfas = false;

    })
    // 案件撤案
    .on('click', '.distribution', function(){
        if(lockfas)return;
        lockfas = true;
        arrcaseid = [];
        if($("#withdraw_case_ids").val()==0){
            toastr.error('请选择案件');
            return false;
        }
        $.ajax({
            url:"/casecenter/withdraw",
            method:"post",
            data:$("#withdraw_form").serialize(),
            success:function(res){
                if(res.code==200){
                    lockfas = false;
                    toastr.success("撤案提交成功!");
                    setTimeout(function(){
                        window.location.reload();
                    },2000)
                }else{
                    lockfas = false;
                    toastr.error(res.message);

                }
            }
        })
        lockfas = false;

    })
    //导出调解记录
    .on('click', '.distribution_exportMediate', function(){
        if(lockfas)return;
        lockfas = true;
        arrcaseid = [];
        if($("#export_case_ids").val()==0){
            toastr.error('请选择案件');
            return false;
        }
        $.ajax({
            url:"/casecenter/exportMediateRecord",
            method:"post",
            data:$("#exportMediate_form").serialize(),
            success:function(res){
                if(res.code==200){
                    lockfas = false;
                    $.confirm({
                        title: '注意！',
                        content: '提交导出调解成功，是否跳转导出任务查看下载！',
                        confirmButton: '确定',
                        cancelButton: '取消',
                        confirm: function(){
                            window.location.href = '/export';
                        },
                        cancel: function(){
                            window.location.reload();
                        }
                    });
                }else{
                    lockfas = false;
                    toastr.error(res.message);
                }
            }
        })
        lockfas = false;

    })
    //导出调解记录
    .on('click', '.distribution_exportSms', function(){
        if(lockfas)return;
        lockfas = true;
        arrcaseid = [];
        if($("#exportSms_case_ids").val()==0){
            toastr.error('请选择案件');
            return false;
        }
        $.ajax({
            url:"/casecenter/exportSmsRecord",
            method:"post",
            data:$("#exportSms_form").serialize(),
            success:function(res){
                if(res.code==200){
                    lockfas = false;
                    $.confirm({
                        title: '注意！',
                        content: '提交导出调解成功，是否跳转导出任务查看下载！',
                        confirmButton: '确定',
                        cancelButton: '取消',
                        confirm: function(){
                            window.location.href = '/export';
                        },
                        cancel: function(){
                            window.location.reload();
                        }
                    });
                }else{
                    lockfas = false;
                    toastr.error(res.message);
                }
            }
        })
        lockfas = false;

    })
    //导出调解记录
    .on('click', '.distribution_exportCall', function(){
        if(lockfas)return;
        lockfas = true;
        arrcaseid = [];
        if($("#exportCall_case_ids").val()==0){
            toastr.error('请选择案件');
            return false;
        }
        $.ajax({
            url:"/casecenter/exportCallRecord",
            method:"post",
            data:$("#exportCall_form").serialize(),
            success:function(res){
                if(res.code==200){
                    lockfas = false;
                    $.confirm({
                        title: '注意！',
                        content: '提交导出呼叫记录成功，是否跳转导出任务查看下载！',
                        confirmButton: '确定',
                        cancelButton: '取消',
                        confirm: function(){
                            window.location.href = '/export';
                        },
                        cancel: function(){
                            window.location.reload();
                        }
                    });
                }else{
                    lockfas = false;
                    toastr.error(res.message);
                }
            }
        })
        lockfas = false;

    })
    //导出调解录音
    .on('click', '.distribution_exportFileRecord', function(){
        if(lockfas)return;
        lockfas = true;
        arrcaseid = [];
        if($("#exportFile_case_ids").val()==0){
            toastr.error('请选择案件');
            return false;
        }
        $.ajax({
            url:"/casecenter/exportFileRecord",
            method:"post",
            data:$("#exportFileRecord_form").serialize(),
            success:function(res){
                if(res.code==200){
                    $.confirm({
                        title: '注意！',
                        content: '提交导出录音成功，是否跳转导出任务查看下载！',
                        confirmButton: '确定',
                        cancelButton: '取消',
                        confirm: function(){
                            window.location.href = '/export';
                        },
                        cancel: function(){
                            window.location.reload();
                        }
                    });
                }else{
                    lockfas = false;
                    toastr.error(res.message);
                }
            }
        })
        lockfas = false;

    })
    //更新委案到期时间
    .on('click', '.distribution_entrustExpireTime', function(){
        if(lockfas)return;
        lockfas = true;
        arrcaseid = [];
        if($("#entrustExpireTime_case_ids").val()==0){
            toastr.error('请选择案件');
            return false;
        }
        $.ajax({
            url:"/casecenter/entrustExpireTime",
            method:"post",
            data:$("#entrustExpireTime_form").serialize(),
            success:function(res){
                if(res.code==200){
                    lockfas = false;
                    toastr.success("更新委案时间成功");
                    setTimeout(function(){
                        window.location.reload();
                    },2000)
                }else{
                    lockfas = false;
                    toastr.error(res.message);
                }
            }
        })
        lockfas = false;

    })







